package com.car.advertising.view;

import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;

import com.car.advertising.interfacener.RiseNumberBase;

import java.text.DecimalFormat;

/**
 * 自定义金额动画显示控件
 */
public class RiseNumberTextView extends TextView implements RiseNumberBase {

    public static final int INCREASE = 0;// 增长

    public static final int DECREASE = 1;// 减少

    private int increase;
    private static final int STOPPED = 0;

    private static final int RUNNING = 1;

    private int mPlayingState = STOPPED;

    private double number;

    private String mNum;// 单位

    private double fromNumber;

    private long duration = 1500;
    /**
     * 1.int 2.float
     */
    private int numberType = 2;

    private DecimalFormat fnum;

    private EndListener mEndListener = null;

    final static int[] sizeTable = {9, 99, 999, 9999, 99999, 999999, 9999999, 99999999, 999999999, Integer.MAX_VALUE};

    public RiseNumberTextView(Context context) {
        super(context);
    }

    public RiseNumberTextView(Context context, AttributeSet attr) {
        super(context, attr);
    }

    public RiseNumberTextView(Context context, AttributeSet attr, int defStyle) {
        super(context, attr, defStyle);
    }

    public interface EndListener {
        public void onEndFinish();
    }

    public boolean isRunning() {
        return (mPlayingState == RUNNING);
    }

    private void runDouble() {
        ValueAnimator valueAnimator = ValueAnimator.ofObject(new DoubleEvaluator(), fromNumber, number);
        valueAnimator.setDuration(duration);

        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                if (increase == INCREASE) {
                    setText(mNum + fnum.format(Double.parseDouble(valueAnimator.getAnimatedValue().toString())));
                } else {
                    setText(fnum.format(Double.parseDouble(valueAnimator.getAnimatedValue().toString())));
                }
                if (valueAnimator.getAnimatedFraction() >= 1) {
                    mPlayingState = STOPPED;
                    if (mEndListener != null)
                        mEndListener.onEndFinish();
                }
            }

        });
        valueAnimator.start();
    }

    private void runInt() {
        ValueAnimator valueAnimator = ValueAnimator.ofInt((int) fromNumber, (int) number);
        valueAnimator.setDuration(duration);

        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                if (increase == INCREASE) {
                    setText(mNum + valueAnimator.getAnimatedValue().toString());
                } else {
                    setText(valueAnimator.getAnimatedValue().toString());
                }

                if (valueAnimator.getAnimatedFraction() >= 1) {
                    mPlayingState = STOPPED;
                    if (mEndListener != null)
                        mEndListener.onEndFinish();
                }
            }
        });
        valueAnimator.start();
    }

    public class DoubleEvaluator implements TypeEvaluator {
        public Object evaluate(float fraction, Object startValue, Object endValue) {
            double startDouble = ((Number) startValue).doubleValue();
            return startDouble + fraction * (((Number) endValue).floatValue() - startDouble);
        }
    }

    static int sizeOfInt(int x) {
        for (int i = 0; ; i++)
            if (x <= sizeTable[i])
                return i + 1;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        fnum = new DecimalFormat("##.00");
    }

    @Override
    public void start() {
        if (!isRunning()) {
            mPlayingState = RUNNING;
            if (numberType == 1)
                runInt();
            else
                // runFloat();
                runDouble();
        }

    }

    @Override
    public RiseNumberTextView withNumberDouble(String num, int increase) {
        this.increase = increase;
        if (increase == INCREASE) {
            /* 截掉单位¥22.30的¥ */
            mNum = num.substring(0, 1);
            number = Double.parseDouble(num.substring(1, num.length()).replace(",", ""));
            numberType = 2;
            fromNumber = 0;
        } else {
            fromNumber = Double.parseDouble(num.substring(0, num.length()).replace(",", ""));
            numberType = 2;
            float number = 0;
            this.number = number;
        }
        return this;
    }

    @Override
    public RiseNumberTextView withNumberInt(String num, int increase) {
        if (increase == INCREASE) {
			/* 截掉单位￥22.30的￥ */
            mNum = num.substring(0, 1);
            float number = Float.parseFloat(num.substring(1, num.length()));
            this.number = number;
            numberType = 1;
            if (number > 1000) {
                fromNumber = number - (float) Math.pow(10, sizeOfInt((int) number) - 2);
            } else {
                fromNumber = number / 2;
            }
        } else {
            fromNumber = Float.parseFloat(num.substring(0, num.length()));
            numberType = 1;
            number = 0;
        }
        return this;
    }

    @Override
    public RiseNumberTextView setDuration(long duration) {
        this.duration = duration;
        return this;
    }

    @Override
    public void setOnEnd(EndListener callback) {
        mEndListener = callback;
    }

}
